package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.dto.Person;
import com.example.demo.other.ValidList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ghl
 * @version 1.0
 * @date 2022/9/21 10:03
 * @description 入参字段校验
 */
@RestController
public class TestController {

    private static final Logger logger = LoggerFactory.getLogger(TestController.class);


    /**
     * 校验单一对象
     *
     * @param person
     * @return
     */
    @PostMapping("/postOne")
    public ResponseEntity<Person> testOne(@RequestBody @Validated Person person) {
        logger.info("postOne接收入参[{}]", JSON.toJSONString(person));
        return new ResponseEntity<>(person, HttpStatus.OK);
    }

    /**
     * 校验集合对象
     *
     * @param persons
     * @return
     */
    @PostMapping("/postList")
    public ResponseEntity<List<Person>> testList(@RequestBody @Validated ValidList<Person> persons) {
        logger.info("postList接收入参[{}]", JSON.toJSONString(persons));
        return new ResponseEntity<>(persons, HttpStatus.OK);
    }


}
